package com.glinsoft.ticktak.controller.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@Slf4j
public class CrossDomainInterceptor implements HandlerInterceptor {




    private List<String> includePathPatterns = new ArrayList<>();
    private List<String> excludePathPatterns = new ArrayList<>();



    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 设置允许跨域
        String allowedPath = response.getHeader("Access-Control-Allow-Origin");
        if(StringUtils.isEmpty(allowedPath)){
            response.addHeader("Access-Control-Allow-Origin", "*" );
        }else{
            response.setHeader("Access-Control-Allow-Origin", "*" );
        }
        return true;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView)throws Exception{


    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception{
    }


    /**
     * 将要拦截的路径
     * @return
     */
    public List<String> getIncludePathPatterns() {
        includePathPatterns.add("/**");
        return includePathPatterns;
    }

    /**
     * 排除拦截的路径设置
     * @return
     */
    public List<String> getExcludePathPatterns() {
        return excludePathPatterns;
    }



}